/*
 * Copyright (c) 2002 David Corbin and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    David Corbin - Add support for creating depot "connections" in all cases
 *    Boris Pruessmann - Tweaked behavior
 */
package net.sourceforge.p4eclipse.ui.wizards;

import net.sourceforge.p4eclipse.core.api.FolderRepositoryRelation;
import net.sourceforge.p4eclipse.core.api.PerforceUtil;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import net.sourceforge.p4eclipse.core.api.*;
import net.sourceforge.p4eclipse.ui.*;

/**
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:dcorbin@users.sourceforge.net">David Corbin</a>
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class ConnectProjectPage extends PerforceWizardPage
{
	ClientForm clientForm;
	FolderRepositoryRelation relation;
	IPath currentProjectPath;
	private static final String MSG_PROJECT_IN_DEPOT = 
			"A folder is already in the depot at this location. "
			+"You are strongly urged to verify before completing the sharing process.";
	;
	static final String MSG_NOT_BENEATH_ROOT =
		"The Project Location must be beneath your client root";
	PerforceServer server;
	MicroPlugin plugin;
	
	protected ConnectProjectPage(PerforceServer server,MicroPlugin plugin,String projectPathName)
	{
		super("mappingPage");
		this.server = server;
		clientForm = server.getClient();
		currentProjectPath = new Path(projectPathName);
		setTitle("Adjust project location");
		setImageDescriptor(plugin.getImageDescriptor(PerforceUIPlugin.IMG_REPOSITORY));
		this.plugin = plugin;
	}

	static protected Text createText(Composite parent) 
	{
		Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
		
		GridData data = new GridData(GridData.FILL_BOTH);
		data.grabExcessVerticalSpace = false;
		data.grabExcessHorizontalSpace = true;
		data.widthHint = IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH;

		text.setLayoutData(data);
		return text;
	}
	static private Button createButton(Composite parent,String label) 
	{
		Button button = new Button(parent, SWT.PUSH|SWT.BORDER);
		button.setText(label);
		return button;
	}

	Text location;
	Button locationButton;
	Label projectLocationWarning;
			
//	Text remoteLocation;
//	Button remoteLocationButton;
	
	public void setErrorMessage(String msg)
	{
		super.setErrorMessage(msg);
		setPageComplete(msg == null);
	}
	private void adjustControls(String pathName)
	{
		if (pathName.equals(""))
		{
			adjustControls(FolderRepositoryRelation.OUTSIDE_ROOT);
			return;
		}

		IPath path = new Path(pathName);
		IPath workspaceRoot = plugin.getWorkspaceRoot().getLocation();

		projectLocationWarning.setText(moveMessageText(path));
		
		if (path.isPrefixOf(workspaceRoot))
		{
			setErrorMessage("The project cannot be a parent of the workspace.");
			return;
		}
		
		adjustControls(server.determineFolderPosition(pathName));
		projectLocationWarning.getParent().pack(true);		
	}
	private String moveMessageText(IPath path)
	{
		if (path.equals(currentProjectPath))
			return "";
		return "Your project will be moved";
	}
	private void adjustControls(FolderRepositoryRelation newRelation)
	{
		if (newRelation == FolderRepositoryRelation.IN_VIEW)
		{
			setErrorMessage(null);
			return;
		}
		if (newRelation == FolderRepositoryRelation.IN_REPOSITORY)
		{
			setErrorMessage(null);
			projectLocationWarning.setText(MSG_PROJECT_IN_DEPOT);
			return;
		}
		if (newRelation == FolderRepositoryRelation.OUTSIDE_ROOT)
			setErrorMessage(MSG_NOT_BENEATH_ROOT+" ("+clientForm.getRoot()+")");
		else if (newRelation == FolderRepositoryRelation.UNDER_ROOT)
			setErrorMessage("The Project Location requires manual editing of your Perforce client");
		else
			setErrorMessage("Unexpected response from Perforce server");
			
	}
	public boolean canFinish()
	{
		return getErrorMessage() == null;
	}
	public void createControl(final Composite parent)
	{
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();

		GridData data = new GridData(GridData.FILL_BOTH);
		composite.setLayout(layout);
		composite.setLayoutData(data);
		layout.numColumns = 3;
	
		createLabel(composite,"Project Location:");
		location = createText(composite);
		location.setEditable(false);
		locationButton = createButton(composite,"Browse...");

		projectLocationWarning = createCenteredLabel(composite);
		data = new GridData();
		projectLocationWarning.setLayoutData(data);
		data.horizontalSpan = 3;
		data.verticalSpan = GridData.VERTICAL_ALIGN_FILL;
		data.horizontalAlignment = GridData.CENTER;
		data.widthHint = 350;
		data.heightHint = 50;
				
		linkProjectLocationControls(parent);
	
		location.setText(currentProjectPath.toString());
	
//		createLabel(composite,"Remote Location:");
//		remoteLocation = createText(composite);
//		remoteLocationButton = createButton(composite,"Browse...");

		setControl(composite);		
	}

	private void linkProjectLocationControls(final Composite parent) 
	{
		locationButton.addSelectionListener(new SelectionListener()
		{
			public void widgetDefaultSelected(SelectionEvent event)
			{
				widgetSelected(event);
			}
			public void widgetSelected(SelectionEvent event)
			{	
				DirectoryDialog dialog = new DirectoryDialog(parent.getShell());
				String start = location.getText();
				if (start == null || "".equals(start))
					start = clientForm.getRoot();
		
				dialog.setFilterPath(start);
				String pathString = dialog.open();
		
				if (pathString != null)
				{
					IPath path = new Path(pathString).append(currentProjectPath.lastSegment());
					location.setText(path.toString());
				}
			}
		});
		
		location.addModifyListener(new ModifyListener()
			{
				public void modifyText(ModifyEvent event)
				{
					adjustControls(location.getText());
				}
			}
			);
	}

	public IPath projectLocation() 
	{
		return new Path(location.getText());
	}
}
